﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TemplateGenerator.Entities;
using TemplateGenerator.Logic;

namespace TemplateGenerator.Controls
{
    /// <summary>
    /// Interaction logic for ImportControl.xaml
    /// </summary>
    public partial class ImportControl : Window
    {
        public ImportControl()
        {
            InitializeComponent();
        }

        private void btnFileBrowse_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".docx";
            dlg.Filter = "docx/doc files (.docx; .doc)|*.docx;*doc";
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                txtFile.Text = dlg.FileName;
            }
        }

        private void btnImageBrowse_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.InitialDirectory = Environment.CurrentDirectory + "\\Resources\\";
            dlg.DefaultExt = ".png";
            dlg.Filter = "png images (.png)|*.png";
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                txtImage.Text = dlg.FileName;
            }
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            CustomTemplate template = new CustomTemplate();
            template.Version = new List<WordVersion>();

            if (String.IsNullOrWhiteSpace(txtName.Text) || String.IsNullOrWhiteSpace(txtFile.Text) || String.IsNullOrWhiteSpace(txtImage.Text))
            {
                ExceptionHandler.NotifyAboutException(new Exception("All fields are required"));
                return;
            }

            if (!chk2010.IsChecked.Value && !chk2007.IsChecked.Value && !chk2003.IsChecked.Value)
            {
                ExceptionHandler.NotifyAboutException(new Exception("Atleast one of the options has to be selected"));
                return;
            }

            template.Name = txtName.Text.Trim();
            template.FilePath = txtFile.Text;
            template.Image = txtImage.Text;

            if(chk2010.IsChecked.HasValue && chk2010.IsChecked.Value)
                template.Version.Add(WordVersion.Word2010);


            if (chk2007.IsChecked.HasValue && chk2007.IsChecked.Value)
                template.Version.Add(WordVersion.Word2007);
             
            if(chk2003.IsChecked.HasValue && chk2003.IsChecked.Value)
                template.Version.Add(WordVersion.Word2003);

            template.FilePath = GeneratorHandler.CopyFile(template.FilePath);
            template.Image = GeneratorHandler.CopyFile(template.Image);

            Close();
        }
    }
}
